//-----------------------------------------------------------------------
// <copyright file="tblFileUploadDAO.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Robot.Entities;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Robot.Data
{
  public class tblFileUploadDAO : tblFileUploadDAOBase
  {
    public tblFileUploadDAO()
    {
    }

    public virtual tblFileUpload GettblFileUploadByFileName(string sFileName, string peoplewise)
    {
      try
      {
        Database database = DatabaseFactory.CreateDatabase();
        DbCommand dbCommand = database.GetStoredProcCommand("_sptblFileUploadGetByFileName");

        database.AddInParameter(dbCommand, "@FileName", DbType.AnsiString, sFileName);
        database.AddInParameter(dbCommand, "@Peoplewise", DbType.AnsiString, peoplewise);

        tblFileUpload tblFileUpload = null;
        using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
        {
          if (reader.Read())
          {
            tblFileUpload = CreatetblFileUploadFromReader(reader);
            reader.Close();
          }
        }
        return tblFileUpload;
      }
      catch (Exception ex)
      {
        // log this exception
        Logger.Error(ex);
        // wrap it and rethrow
        throw new DataAccessException(SR.DataAccessGettblFileUploadException, ex);
      }
    }

    public virtual tblFileUploadCollection GetRateFileByCreatedDate(DateTime fromDate, DateTime toDate, string peoplewise)
    {
      try
      {
        Database database = DatabaseFactory.CreateDatabase();
        DbCommand dbCommand = database.GetStoredProcCommand("_sptblGetRateFileByCreatedDate");

        database.AddInParameter(dbCommand, "@FromDate", DbType.DateTime, fromDate);
        database.AddInParameter(dbCommand, "@ToDate", DbType.DateTime, toDate);
        database.AddInParameter(dbCommand, "@Peoplewise", DbType.AnsiString, peoplewise);

        tblFileUploadCollection tblFileUploadCollection = new tblFileUploadCollection();
        using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
        {
          while (reader.Read())
          {
            tblFileUpload tblFileUpload = CreatetblFileUploadFromReader(reader);
            tblFileUploadCollection.Add(tblFileUpload);
          }
          reader.Close();
        }
        return tblFileUploadCollection;
      }
      catch (Exception ex)
      {
        // log this exception
        Logger.Error(ex);
        // wrap it and rethrow
        throw new DataAccessException(SR.DataAccessGettblFileUploadException, ex);
      }
    }

    public virtual tblFileUploadCollection GetPDFFileByCreatedDate(DateTime from, DateTime to, string peoplewise)
    {
      try
      {
        Database database = DatabaseFactory.CreateDatabase();
        DbCommand dbCommand = database.GetStoredProcCommand("_sptblGetPDFFileByCreatedDate");

        database.AddInParameter(dbCommand, "@FromDate", DbType.DateTime, from);
        database.AddInParameter(dbCommand, "@ToDate", DbType.DateTime, to);
        database.AddInParameter(dbCommand, "@Peoplewise", DbType.AnsiString, peoplewise);

        tblFileUploadCollection tblFileUploadCollection = new tblFileUploadCollection();
        using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
        {
          while (reader.Read())
          {
            tblFileUpload tblFileUpload = CreatetblFileUploadFromReader(reader);
            tblFileUploadCollection.Add(tblFileUpload);
          }
          reader.Close();
        }
        return tblFileUploadCollection;
      }
      catch (Exception ex)
      {
        // log this exception
        Logger.Error(ex);
        // wrap it and rethrow
        throw new DataAccessException(SR.DataAccessGettblFileUploadException, ex);
      }
    }

    public virtual void DeletetblFileUpload(string peoplewise)
    {
      try
      {
        Database database = DatabaseFactory.CreateDatabase();
        DbCommand dbCommand = database.GetStoredProcCommand("_sptblDeleteRateFileByPeoplewise");

        database.AddInParameter(dbCommand, "@Peoplewise", DbType.AnsiString, peoplewise);

        DataAccessHelper.ExecuteNonQuery(database, dbCommand);
      }
      catch (Exception ex)
      {
        // log this exception
        Logger.Error(ex);
        // wrap it and rethrow
        throw new DataAccessException(SR.DataAccessDeletetblFileUploadException, ex);
      }
    }

  }
}
